import fs from 'fs';
import path from "path"

const __dirname = path.resolve();

const downloadDocs = async () => {

  const promise = new Promise((resolve, reject) => {
    fs.readFile(__dirname + '/docs/新建文本文档.txt', (err, data) => {
      if (err) {
        console.error(err);
        reject('error')
        return;
      }
      resolve(data)
    });
  })

  try {
    const resData = await promise;
    return {
      code: 200,
      data: resData,
      responseHeader: {
        "Content-Type": 'text/plain; charset=utf-8'
      }
    }
  } catch (err) {
    return {
      code: 500,
      data: '',
      responseHeader: {
        "Content-Type": 'text/plain; charset=utf-8'
      }
    }
  }
}

const downloadNoteTemplate = async () => {
  try {
    const data = fs.readFileSync(__dirname + '/docs/Note模板.xlsx');
    return {
      code: 200,
      data: data,
      responseHeader: {
        "Content-Type": 'application/vnd.ms-excel; charset=utf-8'
      }
    }
  } catch (err) {
    return {
      code: 500,
      data: '',
      responseHeader: {
        "Content-Type": 'application/vnd.ms-excel; charset=utf-8'
      }
    }
  }
}


export default {
  downloadDocs,
  downloadNoteTemplate,
}